<template>
	<div class="div1">
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>咨询师管理</el-breadcrumb-item>
			<el-breadcrumb-item>订单管理</el-breadcrumb-item>
		</el-breadcrumb>
	</div>
	<div>
		<div class="div2">
			<span class="spanTop">咨询师名称</span>
			<el-input size="large" v-model="searchName" style="width: 240px" clearable @clear="query"
				placeholder="请输入咨询师名称" />
			<span class="spanTop">用户名</span>
			<el-input size="large" v-model="searchUserName" style="width: 240px" clearable @clear="query"
				placeholder="请输入用户名" />
			<span class="spanTop">收费类别</span>
			<el-input size="large" v-model="searchSort" style="width: 240px" clearable @clear="query"
				placeholder="请输入收费类别" />
			<span class="spanTop">金额</span>
			<el-input size="large" v-model="searchMoney" style="width: 240px" clearable @clear="query"
				placeholder="请输入金额" />
			<el-button @click="query" type="primary" size="large" class="bt1">搜索</el-button>
			<el-button type="danger" size="large" class="bt1" @click="removeAll">批量删除</el-button>
		</div>

		<el-table :data="tableData" style="width:100%; margin-top: 10px;" border
			@selection-change="handleSelectionChange">
			<el-table-column type="selection" />
			<el-table-column fixed prop="id" label="ID" />
			<el-table-column prop="counselorFee.name" label="收费类别" />
			<el-table-column prop="actualPayment" label="金额" />
			<el-table-column prop="sysUser.username" label="用户名" />
			<el-table-column prop="time" label="下单时间" />
			<el-table-column prop="appointmentTime" label="咨询预约时间" />
			<el-table-column prop="counselor.name" label="咨询师名称" />
			<el-table-column fixed="right" label="操作" width="200">
				<template #default="scope">
					<el-button type="danger" @click="remove(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10,15, 20]"
			:size="size" layout="total, sizes, prev, pager, next, jumper" :total=" total"
			@size-change="handleSizeChange" @current-change="handleCurrentChange" v-if="total !=0" />
	</div>
</template>
<script setup>
	//数据太少，删除接口不是正确的
	import {
		onMounted,
		ref,
		reactive,
		computed,
		watch
	} from 'vue'
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'
	import {
		Plus
	} from '@element-plus/icons-vue'
	import {
		getCurrentInstance
	} from 'vue';

	const axios = getCurrentInstance().appContext.config.globalProperties.axios
	const tableData = ref([])
	const currentPage = ref(1) // 当前页
	const pageSize = ref(5) // 每页显示条目数
	const size = 'default' // 分页大小
	const total = ref(0)
	const searchName = ref('')
	const searchUserName = ref('')
	const searchSort = ref('')
	const searchMoney = ref('')

	const form = ref({
		sysUserId: "",
		counselorId: "",
		chargingId: "",
		time: "",
		appointmentTime: "",
		actualPayment: "",
		del: "",
		id: "",
	})

	const dialogFormVisible = ref(false)
	const formLabelWidth = '100px'
	const AllCounselorLevel = ref([])

	const getAllCounselorLevel = () => {
		axios.get('CounselorOrder', ).then((resp) => {
			console.log(resp.data);
			AllCounselorLevel.value = resp.data
		});
	}

	function query() {
		axios.get(
			`CounselorOrder/page?counselor.name=${searchName.value}&sysUser.username=${searchUserName.value}&counselorCFee.name=${searchSort.value}&price=${searchMoney.value}&pageNum=${currentPage.value}&pageSize=${pageSize.value}`,
		).then(resp => {
			tableData.value = resp.data.list;
			total.value = resp.data.total
			console.log(resp);
		})
	}

	const handleSizeChange = (val) => {
		pageSize.value = val;
		query();
	}
	const handleCurrentChange = (val) => {
		currentPage.value = val
		query();
	}
	const remove = (row) => {
		console.log('当前行的数据', row);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'Warning', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				axios.delete(`CounselorOrder/${row.id}`).then((resp) => {
					console.log('删除后：', resp);
					if (resp.data.code == 200) {
						ElMessage({
							message: '删除成功',
							type: 'success',
						})
					} else {
						ElMessage.error('删除失败')
					}
				}).finally(() => {
					query();
				});

			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})
	}
	const multipleSelection = ref([])
	const handleSelectionChange = (val) => {
		multipleSelection.value = val.map(item => {
			return item.id
		})
		console.log(multipleSelection.value);
	}
	const removeAll = (row) => {
		console.log('当前行的数据', row);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'Warning', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				axios.delete(`CounselorOrder`, {
					params: {
						ids: multipleSelection.value.join(',')
					}
				}).then(resp => {
					if (resp.code == 200) {
						ElMessage({
							message: '删除成功',
							type: 'success',
						})
					} else {
						ElMessage.error('删除失败')
					}
				}).finally(() => {
					query();
				});
			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})
	}
	onMounted(() => {
		query();
		getAllCounselorLevel();
	})
</script>
<style scoped>
	.spanTop {
		margin: 0 10px;
		color: #696a6e;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}

	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}

	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}

	.bt1 {
		margin-top: 10px;
		margin-left: 10px;
	}

	.div1 {
		border-bottom: 1px solid #eeeeee;
		width: 100%;
		height: 30px;
		padding-top: 15px;
		padding-left: 15px;
	}

	.div2 {
		padding-top: 15px;
	}

	.el-table {
		margin-left: 10px;
	}
	.el-pagination{
		margin-top: 10px;
		margin-left: 10px;
	}
</style>